package com.ethink.platform.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.mdm.api.MateFacade;
import com.ethink.mdm.api.component.MdmComponent;
import com.ethink.mdm.api.info.CurrencyInfo;
import com.ethink.mdm.api.info.MateSkuInfo;
import com.ethink.order.api.constant.OrderRespCode;
import com.ethink.order.api.model.SaleDemandExcelModel;
import com.ethink.platform.controller.mdm.info.MateSkuSaleDetailInfo;
import com.ethink.platform.controller.order.info.SaleDemandImportInfo;
import com.ethink.warehouse.api.component.WarehouseComponent;
import com.ethink.warehouse.api.info.MateSkuStockInfo;
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.ethink.order.api.constant.OrderConstant.*;

/**
 * @author default
 * @version 1.0.0
 * @ClassName SaleDemandManager.java
 * @Description
 * @createTime 2022年11月11日 17:24:00
 */
@Component
public class SaleDemandManager {


    @Autowired
    private ExcelImportManager excelImportManager;

    @Autowired
    private MateFacade mateFacade;

    @Autowired
    private WarehouseComponent warehouseComponent;

    @Autowired
    private PublicManager publicManager;

    public List<SaleDemandImportInfo> saleDemandImport(MultipartFile file, String saleSubChannel, String currencyCode) {
        List<SaleDemandExcelModel> saleDemandExcelModels = excelImportManager.transferClass(file, SaleDemandExcelModel.class);
        if (CollUtil.isEmpty(saleDemandExcelModels)) {
            return CollUtil.newArrayList();
        }
        List<String> mateSkuCodeList = CollUtil.getFieldValues(saleDemandExcelModels, "mateSkuCode", String.class);
        List<MateSkuInfo> mateSkuInfoList = mateFacade.queryMateSkuBySkuCodeList(mateSkuCodeList);
        if (saleDemandExcelModels.size() != mateSkuInfoList.size()) {
            throw new BusinessException(OrderRespCode.MATE_SKU_EXCEL_IS_NOT_EXIST);
        }
        Map<String, MateSkuStockInfo> stockInfoMap = warehouseComponent.queryMateSkuStore(mateSkuCodeList);
        Map<String, BigDecimal> taxRateAndDiscount = publicManager.getTaxRateAndDiscount(saleSubChannel);
        BigDecimal discount = taxRateAndDiscount.get("discount");
        BigDecimal taxRate = taxRateAndDiscount.get("taxRate");
        BigDecimal exchangeRate = publicManager.getExchangeRate(currencyCode);
        List<SaleDemandImportInfo> importInfoList = saleDemandExcelModels.stream().map(
                m -> {
                    SaleDemandImportInfo importInfo = new SaleDemandImportInfo();
                    importInfo.setDemandPeriod(m.getDemandPeriod());
                    importInfo.setDemandQty(m.getDemandQty());
                    importInfo.setEndTime(m.getEndTime());
                    importInfo.setExceptSalePriceTax(m.getExceptSalePriceTax());
                    importInfo.setTargetCostLocal(m.getTargetCostLocal());
                    importInfo.setWorstDemandPeriod(m.getWorstDemandPeriod());
                    if (m.getExceptSalePriceTax() != null) {
                        importInfo.setExceptSalePriceNoTax(publicManager
                                .noTaxCount(m.getExceptSalePriceTax(), taxRate, discount));
                        importInfo.setExceptSalePriceNoTaxLocal(publicManager.multiply(importInfo.getExceptSalePriceNoTax(), exchangeRate));
                        importInfo.setExceptSalePriceTaxLocal(publicManager.multiply(importInfo.getExceptSalePriceTax(), exchangeRate));
                    }
                    importInfo.setDiscount(discount);
                    importInfo.setTaxRate(taxRate);
                    mateSkuInfoList.stream().filter(m2 -> StrUtil.equals(m.getMateSkuCode(), m2.getMateSku())).forEach(
                            m3 -> {
                                BeanUtil.copyProperties(m3, importInfo);
                                MateSkuStockInfo skuStockInfo = stockInfoMap.get(m3.getMateSku());
                                importInfo.setTotalQty(skuStockInfo == null ? 0 : skuStockInfo.getTotalQty());
                                importInfo.setPurchasingQty(skuStockInfo == null ? 0 : skuStockInfo.getPurchasingQty());
                            }

                    );
                    return importInfo;
                }).collect(Collectors.toList());
        return importInfoList;
    }

}
